package cn.uniquesoft.service.base;

import java.util.List;

import org.springframework.stereotype.Service;

import cn.uniquesoft.mapper.base.BaseMapper;
import cn.uniquesoft.page.DataGrid;
import cn.uniquesoft.vo.base.PageVO;

@SuppressWarnings({ "unchecked", "rawtypes" })
@Service
public class BaseServiceImpl<T> implements IBaseService<T> {

	protected BaseMapper baseMapper;
	@Override
	public int save(T item) {

		return this.baseMapper.insert(item);
	}

	@Override
	public int remove(T item) {
		return this.baseMapper.delete(item);
	}

	@Override
	public int removeById(int id) {
		return this.baseMapper.deleteById(id);
	}

	@Override
	public int removeByIds(String ids) {
		return this.baseMapper.deleteByIds(ids);
	}

	@Override
	public int edit(T item) {
		return this.baseMapper.update(item);
	}

	@Override
	public T findOne(T item) {
		return (T) this.baseMapper.findOne(item);
	}

	@Override
	public T findOneById(int id) {
		return (T) this.baseMapper.findOneById(id);
	}

	@Override
	public List<T> findAll(T item) {
		return this.baseMapper.findAll(item);
	}

	@Override
	public DataGrid pageQuery(T item) {
		
		PageVO pageVO = (PageVO) item;
		if (pageVO.getPage() <= 0) {
			pageVO.setPage(1);
		}

		pageVO.setStart((pageVO.getPage() - 1) * pageVO.getRows());
		pageVO.setPageSize(pageVO.getRows());
		List<T> rows = this.baseMapper.queryPage(item);
		int total = this.baseMapper.queryPageSum(item);
		DataGrid dataGrid = new DataGrid();
		dataGrid.setRows(rows);
		dataGrid.setTotal((long) total);
		return dataGrid;
	}

}
